<template>
    <div class="singer-song">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
            <div v-for="(item, index) in list" :key="index">
                <h4 class="name">{{ item.name }}</h4>
                <div class="song">
                    <span v-for="(singerItem, index) in item.singer" :key="index" class="singer">
                        {{ singerItem.name }}
                        <span v-show="index != item.singer.length - 1">/</span>
                    </span>
                    <span v-show="item.album" class="albumname">
                        <span class="dot">.</span>
                        <span>{{ item.album.name }}</span>
                    </span>
                    <span  v-show="item.album.subtitle.length != 0">
                        <span class="dot">.</span>
                        <span class="albumsub">{{ item.album.subtitle }}</span>
                    </span>
                </div>
            </div>
        </van-list>
    </div>
</template>


<script>
export default {
    // 通过props接收父组件传递的数据 <SingerSong :singerId="id"/>
    props: ["singerId"],
    data() {
        return {
            list: [],// vant的list列表数据
            loading: false,
            finished: false,
            page: 1,// 获取api数据的页码
        }
    },
    created() {
        this.getData()
    },
    methods: {
        getData() {
            this.$request("get", "/singer/songs?singermid=" + this.singerId + "&page=" + this.page + "&num=30")
                .then((res) => {
                    console.log(res)
                    // res.data.list中是分页获取的数据，30个，把这个数据添加到vant的list列表数据中
                    this.list = this.list.concat(res.data.list);
                    // 加载状态结束
                    this.loading = false;
                    // 页码自动加1
                    this.page++;
                    // 数据全部加载完成 (如果页码加1之后获取到的data.list的长度为0，代表数据加载完成，没有数据)
                    if (res.data.list.length == 0) {
                        this.finished = true;
                    }
                });
        },
        onLoad() {
            setTimeout(() => {
                this.getData();// 调用获取分页数据的方法
                this.loading = true;// 正在加载
            }, 1000);
        }
    }
}
</script>
<style scoped>
*{
    padding: 0;
    margin: 0;
}
.singer-song{
    text-align: left;
    padding: 0 1rem; 
}
.song{
    margin-bottom: 1rem;
    color: slategrey;
    font-size: 0.5rem;
}
.dot{
    display: inline-block;
    color: red;
    margin-right: 0.3rem;
    margin-left: 0.3rem;
    font-size: 1.5rem;
    vertical-align: middle;
    margin-top: -0.8rem;
}
</style>
